#pragma once

namespace http {

/**
 * Implementation of POST application/x-www-form-urlencoded request.
 */
class FormUrlEncodedRequest
{
public:
	FormUrlEncodedRequest();

	FormUrlEncodedRequest& AddKeyValue(const char* key, const char* value);
	FormUrlEncodedRequest& AddKeyValue(const char* key, const wchar_t* value);
	FormUrlEncodedRequest& AddKeyValue(const char* key, const CStringW &value);
	FormUrlEncodedRequest& AddKeyValue(const char* key, int value);

	CString GetHead() const;
	const CStringA &GetBody() const;
	HRESULT GetBody(CComVariant &vtBody) const;

private:
	CStringA m_content;

public:
	class Mapper
	{
	private:
		FormUrlEncodedRequest &request;
		const char* key;

	public:
		Mapper(FormUrlEncodedRequest &req, const char* pszKey) : request(req), key(pszKey) {}
		Mapper(FormUrlEncodedRequest *pReq, const char* pszKey) : request(*pReq), key(pszKey) {}

		FormUrlEncodedRequest &operator = (const char* value)
		{
			return request.AddKeyValue(key, value);
		}

		FormUrlEncodedRequest &operator = (const wchar_t* value)
		{
			return request.AddKeyValue(key, value);
		}

		FormUrlEncodedRequest &operator = (const CStringW &value)
		{
			return request.AddKeyValue(key, value);
		}

		FormUrlEncodedRequest &operator = (int value)
		{
			return request.AddKeyValue(key, value);
		}
	};

public:
	Mapper operator[](const char* key)
	{
		return FormUrlEncodedRequest::Mapper(this, key);
	}
};

} // namespace http
